package com.wudl.govern.controller;

import com.wudl.common.api.CommonResult;

import com.wudl.govern.domain.MetaForeignkey;
import com.wudl.govern.service.MetaForeignkeyService;
import com.wudl.govern.vo.MetaForeignkeyVO;
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;

/**
 * meta_foreignkey Service
 *
 * @author wudl
 */
@Api
@RestController
@RequestMapping("/metaForeignkey")
public class MetaForeignkeyController {

    @Autowired
    private MetaForeignkeyService metaForeignkeyService;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public CommonResult<List<MetaForeignkeyVO>> page(@RequestBody MetaForeignkeyVO pageVO, @RequestParam Integer page, @RequestParam Integer size) {

        List<MetaForeignkeyVO> MetaForeignkeyVoList = new ArrayList<>();
  // 拷贝结果
        List<MetaForeignkey> MetaForeignkeyList = metaForeignkeyService.page(page, size);

        for (MetaForeignkey metaForeignkeyTmp:MetaForeignkeyList )
        {
            MetaForeignkeyVO metaForeignkeyVO = new MetaForeignkeyVO();
            BeanUtil.copyProperties(metaForeignkeyTmp, MetaForeignkeyVO.class);
            MetaForeignkeyVoList.add(metaForeignkeyVO);
        }
        return CommonResult.success(MetaForeignkeyVoList);


    }

    /**
     * 查询详情
     */
    @GetMapping("/get/{id}")
    public CommonResult<MetaForeignkeyVO> get(@PathVariable Object id) {
        MetaForeignkey entity = metaForeignkeyService.getById(id);
        return CommonResult.success(BeanUtil.copyProperties(entity, MetaForeignkeyVO.class));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public CommonResult<Void> add(@RequestBody MetaForeignkeyVO addVO) {
        metaForeignkeyService.save(BeanUtil.copyProperties(addVO, MetaForeignkey.class));
        return CommonResult.success();
    }

    /**
     * 编辑
     */
    @PutMapping("/edit")
    public CommonResult<Void> edit(@RequestBody MetaForeignkeyVO editVO) {
        metaForeignkeyService.updateById(BeanUtil.copyProperties(editVO, MetaForeignkey.class));
        return CommonResult.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public CommonResult<Void> delete(@PathVariable Object id) {
        metaForeignkeyService.removeById(id);
        return CommonResult.success();
    }

}
